package com.frxuexi.park

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.frxuexi.lib_base.entity.ParkBean
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.Res
import com.frxuexi.lib_base.net.ApiService
import com.frxuexi.lib_base.net.VideoApiService

/**
 * para：请求网络的参数值
 * type：请求哪个接口
 */
class ParkDataSource (val apiService: ApiService, val para:String, val type:String): PagingSource<Int, ParkBeanItem>() {
    override fun getRefreshKey(state: PagingState<Int, ParkBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ParkBeanItem> {
        try {
            var res:Res<ParkBean>?=null
            if(type=="类型") {
                res = apiService.getParkByType(para)
            }else if(type=="状态"){
                res=apiService.getParkByStatus(para)
            }else if(type=="区域"){
                res=apiService.getParkByArea(para.toInt())
            }
            return LoadResult.Page(res?.data!!,null,null)
        } catch (e: Exception) {
            return LoadResult.Error(e)
        }
    }
}